/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    bool isEvenOddTree(TreeNode *root)
    {
        vector<TreeNode *> layer1;
        vector<TreeNode *> layer2;
        layer1.push_back(root);
        bool isEvenLayer = true;
        while (!layer1.empty())
        {
            if (isEvenLayer)
            {
                int lastVal = 0;
                for (TreeNode *tree : layer1)
                {
                    if (!(tree->val & 1) || (tree->val) <= lastVal)
                    {
                        return false;
                    }
                    lastVal = tree->val;
                    if (tree->left != nullptr)
                    {
                        layer2.push_back(tree->left);
                    }
                    if (tree->right != nullptr)
                    {
                        layer2.push_back(tree->right);
                    }
                }
            }
            else
            {
                int lastVal = INT_MAX;
                for (TreeNode *tree : layer1)
                {
                    if ((tree->val & 1) || (tree->val) >= lastVal)
                    {
                        return false;
                    }
                    lastVal = tree->val;
                    if (tree->left != nullptr)
                    {
                        layer2.push_back(tree->left);
                    }
                    if (tree->right != nullptr)
                    {
                        layer2.push_back(tree->right);
                    }
                }
            }
            isEvenLayer = !isEvenLayer;
            layer1.clear();
            layer1.swap(layer2);
        }
        return true;
    }
};